﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToBinary.CApExportToBinary
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToBinary;

public class CApExportToBinary
{
  private CApExportToBinaryViewModel m_viewModel;
  private ITxExportGraphicsAnimationFileHandlerEx m_fileHandler;

  public event ExportInitiationDone ExportInitiationDoneEvent;

  public CApExportToBinary(
    CApExportToBinaryViewModel apVM,
    ITxExportGraphicsAnimationFileHandlerEx fileHandler = null)
  {
    this.m_viewModel = apVM;
    this.m_fileHandler = fileHandler;
  }

  public bool Launch(
    out HashSet<string> idList,
    out Dictionary<ITxObject, string> objectToFileNameMap)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    TxApplication.ActiveSelection.Clear();
    objectToFileNameMap = new Dictionary<ITxObject, string>();
    idList = new HashSet<string>();
    try
    {
      TxObjectList txObjectList = this.m_viewModel.Roots;
      if (txObjectList == null || ((Collection<ITxObject>) txObjectList).Count == 0)
      {
        txObjectList = new TxObjectList();
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) TxApplication.ActiveDocument.PhysicalRoot);
      }
      TxExportGraphicsAnimationModuleEx animationModuleEx = new TxExportGraphicsAnimationModuleEx(new TxExportGraphicsAnimationInitData(txObjectList, this.m_fileHandler, "", false, this.m_viewModel.SelectedLOD, false)
      {
        ExportData = new TxExportGraphicsAnimationData(),
        FileFormat = (TxExportGraphicsAnimationFileFormatEx) 4,
        ExportEntitiesIndividiualy = this.m_viewModel.ExportEntitiesIndividiualy,
        IncludeCurves = this.m_viewModel.IncludeCurves
      });
      animationModuleEx.Initialize();
      TxObjectList objectList = animationModuleEx.ObjectList;
      ExportInitiationDone initiationDoneEvent = this.ExportInitiationDoneEvent;
      if (initiationDoneEvent != null)
        initiationDoneEvent(objectList);
      animationModuleEx.Export();
      foreach (DictionaryEntry animationObjectName in TxExportGraphicsAnimationEx.GetAnimationObjectNames())
      {
        ITxObject key = animationObjectName.Key as ITxObject;
        objectToFileNameMap.Add(key, animationObjectName.Value as string);
        idList.Add(key.Id);
      }
    }
    catch (Exception ex)
    {
      return false;
    }
    TxApplication.ActiveSelection.SetItems(allItems);
    Cursor.Current = current;
    return true;
  }

  public bool Launch()
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    TxObjectList txObjectList1 = (TxObjectList) null;
    if (this.ShouldClearSelection())
    {
      txObjectList1 = TxApplication.ActiveSelection.GetAllItems();
      TxApplication.ActiveSelection.Clear();
    }
    try
    {
      TxObjectList txObjectList2 = this.m_viewModel.Roots;
      if (txObjectList2 == null || ((Collection<ITxObject>) txObjectList2).Count == 0)
      {
        txObjectList2 = new TxObjectList();
        ((Collection<ITxObject>) txObjectList2).Add((ITxObject) TxApplication.ActiveDocument.PhysicalRoot);
      }
      TxExportGraphicsAnimationInitData animationInitData = this.m_fileHandler == null ? new TxExportGraphicsAnimationInitData(txObjectList2, this.m_viewModel.OutputFileName, "", false, this.m_viewModel.SelectedLOD, false) : new TxExportGraphicsAnimationInitData(txObjectList2, this.m_fileHandler, "", false, this.m_viewModel.SelectedLOD, false);
      animationInitData.ExportData = new TxExportGraphicsAnimationData();
      animationInitData.FileFormat = (TxExportGraphicsAnimationFileFormatEx) 4;
      TxExportGraphicsAnimationEx.Init(animationInitData);
      animationInitData.Dispose();
    }
    catch (Exception ex)
    {
      return false;
    }
    if (txObjectList1 != null)
      TxApplication.ActiveSelection.SetItems(txObjectList1);
    Cursor.Current = current;
    return true;
  }

  private bool ShouldClearSelection()
  {
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    TxObjectList txObjectList = this.m_viewModel.Roots;
    if (txObjectList == null || ((Collection<ITxObject>) txObjectList).Count == 0)
    {
      txObjectList = new TxObjectList();
      ((Collection<ITxObject>) txObjectList).Add((ITxObject) TxApplication.ActiveDocument.PhysicalRoot);
    }
    TxObjectList second = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) txObjectList)
    {
      if (itxObject is ITxObjectCollection)
        ((TxObjectList<ITxObject>) second).AddRange((TxObjectList<ITxObject>) (itxObject as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxNoTypeFilter()));
      ((Collection<ITxObject>) second).Add(itxObject);
    }
    return new List<ITxObject>((IEnumerable<ITxObject>) new TxObjectList<ITxObject>((TxObjectList<ITxObject>) allItems)).Intersect<ITxObject>((IEnumerable<ITxObject>) second).ToList<ITxObject>().Count > 0;
  }
}
